
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */

/**
 *
 * @author Enyths
 */
public class Artist {
    private int id;

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }
    
    private String nom, prenom, login, password;

    public String getLogin() {
        return login;
    }

    public String getNom() {
        return nom;
    }

    public String getPassword() {
        return "●●●●●●●";
    }

    public String getPrenom() {
        return prenom;
    }

    public void setLogin(String login) {
        this.login = login;
        this.updateLogin();
    }

    public void setNom(String nom) {
        this.nom = nom;
        this.updateNom();
    }

    public void setPassword(String p_password) {
        this.password = md5(p_password);
        this.updatePassword();
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
        this.updatePrenom();
    }

    public Artist(int id, String nom, String prenom, String login, String password) {
        this.id = id;
        this.nom = nom;
        this.prenom = prenom;
        this.login = login;
        this.password = password;
    }
    
    
    private void updateNom() {
         try  
        { 
           Context initCtx = new InitialContext();

           DataSource dataSource = (DataSource)initCtx.lookup("jdbc/DBJeemusic");

           Connection connection =  dataSource.getConnection();
           PreparedStatement requetePreparedMusic = connection.prepareStatement("update auteur set nom = ? where id  = ?");

           requetePreparedMusic.setString(1, nom);
           requetePreparedMusic.setInt(2, id);
           requetePreparedMusic.executeUpdate();

           connection.close();
        }
        catch (Exception ex){ex.printStackTrace();
            
        }
    }
    private void updatePrenom() {
         try  
        { 
           Context initCtx = new InitialContext();

           DataSource dataSource = (DataSource)initCtx.lookup("jdbc/DBJeemusic");

           Connection connection =  dataSource.getConnection();
           PreparedStatement requetePreparedMusic = connection.prepareStatement("update auteur set prenom = ? where id  = ?");

           requetePreparedMusic.setString(1, prenom);
           requetePreparedMusic.setInt(2, id);
           requetePreparedMusic.executeUpdate();

           connection.close();
        }
        catch (Exception ex){ex.printStackTrace();
            
        }
    }
    private void updateLogin() {
         try  
        { 
           Context initCtx = new InitialContext();

           DataSource dataSource = (DataSource)initCtx.lookup("jdbc/DBJeemusic");

           Connection connection =  dataSource.getConnection();
           PreparedStatement requetePreparedMusic = connection.prepareStatement("update auteur set login = ? where id = ?");

           requetePreparedMusic.setString(1, login);
           requetePreparedMusic.setInt(2, id);
           requetePreparedMusic.executeUpdate();

           connection.close();
        }
        catch (Exception ex){ex.printStackTrace();
            
        }
    }
    private void updatePassword() {
         try  
        { 
           Context initCtx = new InitialContext();

           DataSource dataSource = (DataSource)initCtx.lookup("jdbc/DBJeemusic");

           Connection connection =  dataSource.getConnection();
           PreparedStatement requetePreparedMusic = connection.prepareStatement("update auteur set password = ? where id = ?");

           requetePreparedMusic.setString(1, password);
           requetePreparedMusic.setInt(2, id);
           requetePreparedMusic.executeUpdate();

           connection.close();
        }
        catch (Exception ex){ex.printStackTrace();
            
        }
    }
    
    
    private String md5(String str) {
        String md5Str = "";
        
        try {
            java.security.MessageDigest msgDigest;
            msgDigest = java.security.MessageDigest.getInstance("MD5");
             msgDigest.update(str.getBytes());
             byte[] digest = msgDigest.digest();
             for (int i = 0; i < digest.length; ++i) {
                 int value = digest[i];
                 if (value < 0) {
                     value += 256;
                 }
                 
                 md5Str += Integer.toHexString(value);
            }
                 this.updatePassword();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Artist.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return md5Str;
    }
    
    
    
    public void save() {
         try  
        { 
           Context initCtx = new InitialContext();

           DataSource dataSource = (DataSource)initCtx.lookup("jdbc/DBJeemusic");

           Connection connection =  dataSource.getConnection();
           PreparedStatement requetePreparedMusic = connection.prepareStatement("INSERT INTO auteur(nom, prenom, login, password) VALUES (?, ?,?,?)");
           requetePreparedMusic.setString(1, nom);
           requetePreparedMusic.setString(2, prenom);
           requetePreparedMusic.setString(3, login);
           requetePreparedMusic.setString(4, password);
           requetePreparedMusic.executeUpdate();

           connection.close();
        }
        catch (Exception ex){ex.printStackTrace();
            
        }
    }
}
